package com.tuni.birthdaymoney.service.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuni.birthdaymoney.persistence.entity.Message;
import com.tuni.birthdaymoney.persistence.repository.MessageRepository;
import com.tuni.birthdaymoney.service.MessageService;

@Service
public class SpringMessageService implements MessageService {

    @Autowired
    private MessageRepository messageRepository;

    @Transactional
    public Message add(Message message) {
        return messageRepository.save(message);
    }

    @Transactional(readOnly = true)
    public boolean exists(Message message) {
        return !messageRepository.findByTypeAndBirthdayManAndBirthdayYear(message.getType(), message.getBirthdayMan(),
                message.getBirthdayYear()).isEmpty();
    }
}